import java.util.Scanner;
public class test631 {
    public static boolean isValid(long number) {
        return (getSize(number) >= 13 && getSize(number) <= 16)
                && (prefixMatched(number, 4) || prefixMatched(number, 5) || prefixMatched(number, 37)
                 || prefixMatched(number, 6))
                && ((sumOfDoubleEvenPlace(number) + sumOfOddPlace(number)) % 10 == 0);
    }
    public static int sumOfDoubleEvenPlace(long number) {
        int sum = 0;
        String numStr = number + "";
        for (int i = numStr.length() - 2; i >= 0; i -= 2) {
            sum += getDigit(Integer.parseInt(numStr.charAt(i) + "") * 2);
        }
        return sum;
    }
    public static int getDigit(int number) {
        if (number < 10) return number;
        return number / 10 + number % 10;
    }
    public static int sumOfOddPlace(long number) {
        int sum = 0;
        String numStr = number + "";
        for (int i = numStr.length() - 1; i >= 0; i -= 2) {
            sum += Integer.parseInt(numStr.charAt(i) + "");
        }
        return sum;
    }
    public static boolean prefixMatched(long number, int d) {
        return getPrefix(number, getSize(d)) == d;
    }
    public static int getSize(long d) {
        return (d + "").length();
    }
    public static long getPrefix(long number, int k) {
        String numStr = number + "";
        if (numStr.length() < k) return number;
        return Long.parseLong(numStr.substring(0, k));
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a credit card number as a long integer: ");
        long number = scanner.nextLong();
        if (isValid(number)) {
            System.out.println(number + " is valid");
        } else {
            System.out.println(number + " is invalid");
        }
        scanner.close();
    }
}

